package com.shuyuntu.controller;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.ArrayList;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shuyuntu.common.controller.JdbcController;
import com.shuyuntu.entity.Dog;
import com.shuyuntu.service.IDogService;

@Controller
@RequestMapping("/dog")
public class Dogcontroller {

	private IDogService dogService;

	@RequestMapping("add")
	@ResponseBody
	public void add(Dog dog) throws Exception {
		Connection conn = JdbcController.getConnection();
		Statement stmt = conn.createStatement();// 创建一个Statement对象
		ResultSet rs = stmt.executeQuery("select max(F_ID) from th_dog");
		ResultSetMetaData m = rs.getMetaData();
		int columns = m.getColumnCount();
		//查询最大值id
		while (rs.next()) {
			for (int i = 1; i <= columns; i++) {
				int max = Integer.valueOf(rs.getInt(i));
				dog.setId(max + 1);

			}
		}
		// 生成一条mysql语句
		String sql = "insert into th_dog (F_NAME,F_AGE,F_BIRTHDAY,F_ID)values(" + "'" + dog.getName() + "',"
				+ dog.getAge() + ",'" + dog.getBirthday() + "'," + dog.getId() + ")";
		System.err.println(sql);

		stmt.executeUpdate(sql);// 执行sql语句
	}

	/**
	 * 删除
	 * @param id
	 * @throws Exception
	 */
	@RequestMapping("remove/{id}")
	@ResponseBody
	public void remove(@PathVariable int id) throws Exception {
		System.err.println("id:"+id);
		Connection conn = JdbcController.getConnection();
		Statement stmt = conn.createStatement();// 创建一个Statement对象
		String sql = "DELETE FROM th_dog WHERE F_ID = "+id;
		System.err.println(sql);

		stmt.executeUpdate(sql);// 执行sql语句
		
	}

}
